package com.kstech.ks_gaz01.fragments

import android.text.method.ScrollingMovementMethod
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankii.androidlibrary.base.BaseBindingFragment
import com.blankii.androidlibrary.views.DividerItemDecoration
import com.kstech.ks_gaz01.R
import com.kstech.ks_gaz01.base.HomeViewModel
import com.kstech.ks_gaz01.carChoose.CarsAdapter
import com.kstech.ks_gaz01.currentCarName
import com.kstech.ks_gaz01.databinding.FragmentCarChooseManagerBinding
import com.kstech.ks_gaz01.databinding.LayoutDialogInputaddressBinding
import com.kstech.ks_gaz01.xmlAddress.XmlAddressStatisticUtils

/**
 * author: blankii
 * created on: 2024/4/24 14:46
 * description: 车型管理界面
 */
class CarChooseManagerFragment :
    BaseBindingFragment<FragmentCarChooseManagerBinding>(FragmentCarChooseManagerBinding::inflate) {
    private lateinit var carsList: List<String>
    private lateinit var mViewModel: HomeViewModel

    override fun onCreateInit() {
        carsList = requireActivity().assets.list("cars")!!.toList()
        mViewModel = ViewModelProvider(requireActivity())[HomeViewModel::class.java]
    }

    fun hideXmlAddress() {
        binding.xmlAddressView.visibility = View.GONE
    }

    override fun onInitView() {
        binding.recyclerView.apply {
            this.addItemDecoration(
                DividerItemDecoration(
                    requireActivity(), R.color.background_logoBar
                )
            )
            this.layoutManager = LinearLayoutManager(requireContext())
            this.adapter = CarsAdapter().apply {
                this.submitList(carsList)
                this.setOnItemClickListener { adapter, view, position ->
                    val fileName = carsList[position]
                    if (mViewModel.debugTool) {
                        showAlertDialog(fileName)
                    } else {
                        navigateNext(fileName)
                    }
                }
            }
        }
//        binding.test!!.text = "123\r\n123\r\n1234\r\n124124\r\n1234124124\r\n1241\r\n12412\r\n1241\r\n45151\r\n1412451\r\n422\r\n123\n" +
//                "123\n" +
//                "1234\n" +
//                "124124\n" +
//                "1234124124\n" +
//                "1241\n" +
//                "12412\n" +
//                "1241\n" +
//                "45151\n" +
//                "1412451\n" +
//                "422\n"
//        binding.test.movementMethod=ScrollingMovementMethod.getInstance()
    }

    private fun showAlertDialog(fileName: String) {
        val dialogBinding = LayoutDialogInputaddressBinding.inflate(layoutInflater)
        dialogBinding.editTextNumber.requestFocus()
        val dialogBuilder = AlertDialog.Builder(requireActivity()).setView(dialogBinding.root)
        dialogBuilder.setTitle("请输入需要多少个连续空间个数").setPositiveButton(
            "确定"
        ) { dialog, which ->
            binding.xmlAddressView.visibility = View.VISIBLE
            binding.xmlAddressView.movementMethod = ScrollingMovementMethod.getInstance()
            binding.xmlAddressView.text = "配置文件:  $fileName\n" + "${
                XmlAddressStatisticUtils.start(
                    requireContext(),
                    fileName,
                    dialogBinding.editTextNumber.text.toString().toInt(),
                    dialogBinding.ignoreException.isChecked
                )
            }"
        }
        dialogBuilder.show()
    }

    private fun navigateNext(fileName: String) {
        currentCarName = fileName
        val action =
            CarChooseManagerFragmentDirections.actionCarChooseManagerFragmentToHomeFragment(
                fileName = fileName
            )
        findNavController().navigate(action)
    }

}